<template>
  <page-list :x-uri="uri" :doactions="doactions">
    <!-- 自定义tab -->
    <template v-slot:page-tabs>
      <tabs :course-id="courseId" />
      <page-header :content="headerTitle" />
    </template>
    <!-- 页面操作按钮 -->
    <template v-slot:page-button>
      <el-button v-auth="permission" size="mini" type="primary" @click="create">添加试卷</el-button>
    </template>
    <!-- 多选操作 -->
    <template v-slot:table-button="scope">
      <el-button v-auth="'course.video.delete'" size="mini" @click="deletes(scope)">删除</el-button>
    </template>
  </page-list>
</template>
<script>
import PageList from '@public/template/page_list.vue';
import Tabs from '../tabs';
import PageHeader from '@/components/page-header';
export default {
  components: {
    PageList,
    Tabs,
    PageHeader
  },
  inject: ['reload'],
  data() {
    return {
      headerTitle: '课程考试',
      uri: '/course/admin/exam/' + this.$route.params.id + '/list',
      doactions: [
        {
          title: '考试记录',
          callback: this.record
        },
        {
          title: '编辑',
          callback: this.edit
        },
        { title: '删除', callback: this.delete }
      ]
    };
  },
  computed: {
    courseId() {
      return this.$route.params.id;
    },
    permission() {
      const type = this.$route.query.type;
      const perm = [
        { type: 'video', permission: 'course.video.default.exam' },
        { type: 'live', permission: 'course.live.large.exam' },
        { type: 'live_small', permission: 'course.live.small.exam' },
        { type: 'classes', permission: 'course.classes.default.exam' }
      ];
      const permission = perm.find(item => item.type == type);
      return permission;
    }
  },
  methods: {
    // 添加试卷
    create: function() {
      this.$router.push({ path: '/course/exam/' + this.courseId + '/create' });
    },
    // 考试记录
    record: function({ row }) {
      this.$router.push({ path: '/course/exam/' + this.courseId + '/' + row.paper_id + '/record' });
    },
    // 编辑试卷
    edit: function({ row }) {
      this.$router.push({ path: '/course/exam/' + this.courseId + '/' + row.paper_id + '/edit' });
    },
    // 删除试卷
    delete: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/exam/' + this.courseId + '/' + row.paper_id + '/delete',
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 批量删除试卷
    deletes: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要删除的试卷');
        return false;
      }
      ids = ids.join(',');
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/exam/' + this.courseId + '/deletes/' + ids,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    }
  }
};
</script>
